home *** CD-ROM | disk | FTP | other *** search
/ One Click 21 (Special) / OC021.iso / Juegos / King of Skeleton / King of Skeleton.swf / scripts / __Packages / Vector.as < prev    next >
Encoding:
Text File  |  2006-02-02  |  2.0 KB  |  72 lines

  1. class Vector
  2. {
  3.    var x;
  4.    var y;
  5.    function Vector(i, j)
  6.    {
  7.       this.x = i;
  8.       this.y = j;
  9.    }
  10.    function clone()
  11.    {
  12.       return new Vector(this.x,this.y);
  13.    }
  14.    function magnitude()
  15.    {
  16.       return Math.sqrt(this.x * this.x + this.y * this.y);
  17.    }
  18.    function subtract(v)
  19.    {
  20.       return new Vector(this.x - v.x,this.y - v.y);
  21.    }
  22.    function normalize(l)
  23.    {
  24.       l = l !== undefined ? l : 1;
  25.       var _loc2_ = this.magnitude() / l;
  26.       this.x /= _loc2_;
  27.       this.y /= _loc2_;
  28.    }
  29.    static function vectDist(v1, v2)
  30.    {
  31.       return Math.sqrt((v2.x - v1.x) * (v2.x - v1.x) + (v2.y - v1.y) * (v2.y - v1.y));
  32.    }
  33.    static function cpol(v1, v2, v3)
  34.    {
  35.       var _loc4_ = (v1.x - v2.x) * (v1.x - v2.x) + (v1.y - v2.y) * (v1.y - v2.y);
  36.       var _loc3_ = ((v3.x - v1.x) * (v2.x - v1.x) + (v3.y - v1.y) * (v2.y - v1.y)) / _loc4_;
  37.       return new Vector(v1.x + _loc3_ * (v2.x - v1.x),v1.y + _loc3_ * (v2.y - v1.y));
  38.    }
  39.    static function lineIntersection(av1, av2, bv1, bv2)
  40.    {
  41.       var _loc5_ = av2.y - av1.y;
  42.       var _loc3_ = av1.x - av2.x;
  43.       var _loc9_ = _loc5_ * av1.x + _loc3_ * av1.y;
  44.       var _loc4_ = bv2.y - bv1.y;
  45.       var _loc2_ = bv1.x - bv2.x;
  46.       var _loc8_ = _loc4_ * bv1.x + _loc2_ * bv1.y;
  47.       var _loc1_ = _loc5_ * _loc2_ - _loc4_ * _loc3_;
  48.       if(_loc1_ == 0)
  49.       {
  50.          return undefined;
  51.       }
  52.       return new Vector((_loc2_ * _loc9_ - _loc3_ * _loc8_) / _loc1_,(_loc5_ * _loc8_ - _loc4_ * _loc9_) / _loc1_);
  53.    }
  54.    static function fromPolar(a, m)
  55.    {
  56.       return new Vector(m * Math.cos(a),m * Math.sin(a));
  57.    }
  58.    function toString()
  59.    {
  60.       return "( x=" + this.x + ", y=" + this.y + ")";
  61.    }
  62.    function mark(mc, col)
  63.    {
  64.       col = col != undefined ? col : 16711680;
  65.       mc.lineStyle(0,col);
  66.       mc.moveTo(this.x - 2,this.y - 2);
  67.       mc.lineTo(this.x + 2,this.y + 2);
  68.       mc.moveTo(this.x - 2,this.y + 2);
  69.       mc.lineTo(this.x + 2,this.y - 2);
  70.    }
  71. }
  72.